<?php


namespace Biz\model\wiki;

use Biz\model\advertisement\MBaseDbModel;

class MGoodsAlbum extends MBaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'goods_album';

    protected static $format = true;

    public static function pure()
    {
        self::$format = false;
    }

    public static function getInfo($id)
    {
        $rows = self::select('*', ['id' => $id]);

        return current($rows);
    }

    public static function apiAllList($view = false, $page = 1, $size = 20)
    {
        $where = [
            'status' => self::STATUS_NORMAL,
            'LIMIT' => [($page - 1) * $size, $size]
        ];

        if (!$view) $where['start_time[<=]'] = time();
        $list = (array)MGoodsAlbum::select('*', $where + self::order());
        unset($where['LIMIT']);
        $count = MGoodsAlbum::count($where);
        return ['list' => $list, 'total' => $count];
    }

    public static function apiList($id, $view = false)
    {
        $where = [
            'status' => self::STATUS_NORMAL,
        ];
        if (!$view) $where['start_time[<=]'] = time();
        $list = (array)MGoodsAlbum::select('*', $where + self::order());
        $info = [];
        $total = count($list);
        $index = is_null($id) ? 0 : $id + 1;
        if ($index >= $total) $index = 0;
        if ($total == 1) {
            $info = current($list);
        } else {
            foreach ($list as $key => $v) {
                if ($index == $key) {
                    $info = $v;
                    break;
                }
            }
        }
        $info['index'] = $index;
        return $info;
    }

    public static function order(): array
    {
        return [
            'ORDER' => [
                'start_time' => 'DESC',
                'id' => 'ASC'
            ],
        ];
    }

    public static function select($field, $where = []): array
    {
        $list = parent::select($field, $where);

        if (self::$format && is_array($list)) {
            foreach ($list as &$v) {
                $start_status = $v['start_time'] > time() ? '未上线' : '已上线';
                if ($v['status'] == self::STATUS_DELETE) $start_status = '已下线';
                $v['start_status'] = $start_status;
                $v['start_time'] = date("Y-m-d H:i:s", $v['start_time']);
                $v['wiki_count'] = count((array)explode(',', $v['wiki']));
            }
        }

        return $list;
    }

    public static function isDel($id)
    {
        return self::count(['status' => self::STATUS_DELETE, 'id' => $id]);
    }

    public static function offline($id)
    {
        self::update(['status' => self::STATUS_DELETE], ['id' => $id]);
    }

    public static function online($id)
    {
        self::update(['status' => self::STATUS_NORMAL], ['id' => $id]);
    }

    public static function top($id)
    {
        self::update(['sort' => 0, 'edittime' => time()], ['id' => $id]);
    }


}
